package com.oracle.lingshangejia.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.TaskDetailsAdapter;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.TaskDetailsBean;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 任务详情
 */
public class TaskDetailsActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_title;
    private ImageButton ib_back;
    private TextView tv_taskdetails_content;
    private GridView task_gridView;
    private ArrayList<String> mPicList = new ArrayList<>(); //上传的图片凭证的数据源
    private int outid;
    @Override
    public int intiLayout() {
        return R.layout.activity_taskdetails;
    }

    @Override
    public void initView() {
        tv_title=findViewById(R.id.tv_title);
        ib_back=findViewById(R.id.ib_back);
        ib_back.setOnClickListener(this);
        tv_taskdetails_content=findViewById(R.id.tv_taskdetails_content);
        task_gridView=findViewById(R.id.task_gridView);
        outid=getIntent().getIntExtra("outid",0);
       // Intent intent =getIntent();
        //OutSideBean.DataBean bean=(OutSideBean.DataBean)intent.getSerializableExtra("bean");

        getNetWork();

    }
    private void getNetWork() {
        int ysid= (int) SpUtils.get(TaskDetailsActivity.this,"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("ysid",ysid);
        map.put("rwid",outid);
        OkHttpUtils.getNetWork(TaskDetailsActivity.this, Constant.baseurl+"online/wqrw/wqrwDetail",map,handler);
        //idnumber
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    TaskDetailsBean taskDetailsBean=gson.fromJson(s,TaskDetailsBean.class);
                    if(taskDetailsBean.getCode()==0){
                        tv_taskdetails_content.setText(taskDetailsBean.getData().getRwms());
                        tv_title.setText("任务名称："+taskDetailsBean.getData().getRwmc());
                        if(!taskDetailsBean.getData().getPicUrl1().equals("")||!TextUtils.isEmpty(taskDetailsBean.getData().getPicUrl1())){
                            mPicList.add(Constant.imgurl+taskDetailsBean.getData().getPicUrl1());
                        }
                        if(!taskDetailsBean.getData().getPicUrl2().equals("")||!TextUtils.isEmpty(taskDetailsBean.getData().getPicUrl2())){
                            mPicList.add(Constant.imgurl+taskDetailsBean.getData().getPicUrl1());
                        }
                        if(!taskDetailsBean.getData().getPicUrl3().equals("")||!TextUtils.isEmpty(taskDetailsBean.getData().getPicUrl3())){
                            mPicList.add(Constant.imgurl+taskDetailsBean.getData().getPicUrl1());
                        }
                        if(!taskDetailsBean.getData().getPicUrl4().equals("")||!TextUtils.isEmpty(taskDetailsBean.getData().getPicUrl4())){
                            mPicList.add(Constant.imgurl+taskDetailsBean.getData().getPicUrl1());
                        }
                        if(!taskDetailsBean.getData().getPicUrl5().equals("")||!TextUtils.isEmpty(taskDetailsBean.getData().getPicUrl5())){
                            mPicList.add(Constant.imgurl+taskDetailsBean.getData().getPicUrl1());
                        }
                        if(!taskDetailsBean.getData().getPicUrl6().equals("")||!TextUtils.isEmpty(taskDetailsBean.getData().getPicUrl6())){
                            mPicList.add(Constant.imgurl+taskDetailsBean.getData().getPicUrl1());
                        }

                        TaskDetailsAdapter mGridViewAddImgAdapter = new TaskDetailsAdapter(TaskDetailsActivity.this, mPicList);
                        task_gridView.setAdapter(mGridViewAddImgAdapter);
//                        task_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                            @Override
//                            public void onItemClick(AdapterView<?> parent, View view,
//                                                    int position, long id) {
//                                viewPluImg(position);
//                            }
//                        });
                    }else{
                        Toast.makeText(TaskDetailsActivity.this,taskDetailsBean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(TaskDetailsActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(TaskDetailsActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(TaskDetailsActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    //查看大图
    private void viewPluImg(int position) {
        Intent intent = new Intent(TaskDetailsActivity.this, PlusImageActivity.class);
        intent.putStringArrayListExtra(Constant.IMG_LIST, mPicList);
        intent.putExtra(Constant.POSITION, position);
        startActivityForResult(intent, Constant.REQUEST_CODE_MAIN);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Constant.REQUEST_CODE_MAIN && resultCode == Constant.RESULT_CODE_VIEW_IMG) {

        }
    }
    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                finish();
                break;
        }
    }
}
